//- Copyright (C) 2018-2022 Chatopera Inc, <https://www.chatopera.com>
//- 
//- Licensed under the Apache License, Version 2.0 (the "License");
//- you may not use this file except in compliance with the License.
//- You may obtain a copy of the License at
//- 
//-  http://www.apache.org/licenses/LICENSE-2.0
//- 
//-  Unless required by applicable law or agreed to in writing, software
//- distributed under the License is distributed on an "AS IS" BASIS,
//-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//- See the License for the specific language governing permissions and
//- limitations under the License.
extends /apps/include/layout.pug

block append head
    link(rel="stylesheet", href="/js/ztree/zTreeStyle/zTreeStyle.css")
    script(src="/js/ztree/jquery.ztree.all.min.js")
    script(src="/js/utils.js")
    script(src="/js/lodash-4.17.4.min.js")
    script(src="/js/CSKeFu_Rest_Request.v1.js")
    script.
        cususerid = '#{user.id}';

block content
    include templates/tpl.pug
    if !agentUserList.empty()
        .layui-side.layui-bg-black.layui-left-black
            #agentusers.layui-side-scroll
                include agentusers.pug
        #ukefu-chat-agent.layui-body.ukefu-chat-agent
            //if curagentuser.channel == "phone"
            //    include mainagentuser_callout.pug
            //else if curagentuser.channel == "skype"
            //    include mainagentuser_skype.pug
            //else
            if curagentuser.channel == "messenger"
                include mainagentuser_messenger.pug
            else
                include mainagentuser.pug
    else
        .layui-layout.layui-layout-content(style="height: 100%;")
            .box.default-box(style="height: 100%;")
                .box-header
                    h3.box-title 坐席对话
                .box-body.ukefu-im-theme
                    .ukefu-empty(style="background: none")
                        i.layui-icon 
                        div(style="") 还没有分配记录

    if !agentUserList.empty()
        script(language="javascript").
            $(document).ready(function () {
                $(document).bind("keydown", "Ctrl+return", function (ev) {
                    sendMessage();
                    return false;
                })
            });
            function otherTopicSearch() {
                console.log("otherTopicSearch")
                loadURLWithTip("/agent/other/topic.html?q=" + encodeURIComponent($("#otherSearchQuery").val()), "#topiclist", null, false, true);
            }
            function otherTopicEnter() {
                if (window.event.keyCode == 13) {
                    otherTopicSearch();
                }
            }
            function cleanTopMsgTip(tip) {
                var lastMsgNum = $(tip).find('.last-msg').text();
                if (lastMsgNum) {
                    var num = Number.parseInt(lastMsgNum);
                    Proxy.cleanTopMsgTip(num)
                }
            }
            /**
             * 结束会话，RestAPI请求
             * @param agentUserId
             * @param agentUserName
             */
            function submitEndAgentUser(agentUserId, agentUserName) {
                top.layer.confirm('请确认是否关闭和用户{0}的对话？'.format(agentUserName), {
                        title: "提示",
                        icon: 3
                    },
                    function (index, layero) {
                        top.layer.close(index);
                        restApiRequest({
                            silent: true,
                            path: 'agentuser',
                            data: {
                                ops: 'end',
                                id: agentUserId
                            }
                        }).then(function (result) {
                            if (result.rc === 0) {
                                // 会话关闭成功
                                top.layer.msg('会话已关闭', {icon: 1, time: 1000})
                                //  进行显示调整！
                                if ($("#chat_users li").length > 1) {
                                    $("li[remove-id=" + agentUserId + "]").remove();
                                    $("#chat_users li:first-child a").click();
                                } else {
                                    parent.$('#agentdesktop').click();
                                }
                            } else if (result.rc === 1) {
                                top.layer.msg('不合法的请求参数！', {icon: 2, time: 3000})
                            } else if (result.rc === 2) {
                                top.layer.msg('不合法的操作！', {icon: 2, time: 3000})
                            } else if (result.rc === 3) {
                                top.layer.msg('没有权限执行该操作', {icon: 2, time: 3000})
                            } else if (result.rc === 4) {
                                top.layer.msg('未找到该访客会话', {icon: 2, time: 3000})
                            }
                        }, function (error) {
                            console.log("error", error);
                            // 服务器异常
                            top.layer.msg('服务器抽风，请稍后再试！', {icon: 2, time: 3000})
                        })
                    },
                    function (index, layro) {
                        top.layer.close(index);
                        // cancelled, do nothing
                    })
            }

